package com.shuogg.transaction.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by shuoGG on 2018/6/22
 *
 * 现象: 本机连续访问 man/login man/logout, request和session对象地址一直不变
 *       本机继续访问 user/login user/logout, request和session对象地址一直不变且完全等于访问man那时的地址
 *       另外一台机访问 man/login man/logout, request和session对象地址一直不变, request依旧完全等于上述, 但session不等了
 *
 *       # 实现类: request(RequestFacade) session(StandardSessionFacade)
 *       # session.setAttribute追到最深是StandardSession对ConcurrentMap<String, Object> attributes进行了put(key,value)
 *
 *       # 关掉浏览器再访问man/login 则request.getRequestedSessionId()为null, getRequestedSessionId的实质是对request的cookie.get("JSESSIONID")
 *       # 由于浏览器关了, 而且在response中setCookie("JSESSIONID", value)时没有设置过期时间, 所以关掉时浏览器就把这段cookie清了
 */
@Controller
@RequestMapping("man")
public class ManController {
    /**
     * 测试SessionId
     *
     * @param request
     * @param session
     * @return
     */
    @RequestMapping("login")
    @ResponseBody
    public String login(HttpServletRequest request, HttpSession session) {
        System.out.println(request);
        System.out.println(session);
        System.out.println(request.getRequestedSessionId());
        System.out.println(session.getId());
        session.setAttribute("manId", 12);
        return "登录成功";
    }

    @RequestMapping("logout")
    @ResponseBody
    public String logout(HttpServletRequest request, HttpSession session) {
        System.out.println(request);
        System.out.println(session);
        session.setAttribute("manId", 13);
        return "退出成功";
    }

}
